home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Other Stuff / Networking ƒ / ATP Sample App / packets / network.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-18  |  10.2 KB  |  541 lines  |  [TEXT/KAHL]

  1. #include "packets.h"
  2. #include "atalk.h"
  3.  
  4. #define    ATP_TIMEOUT        2
  5. #define    ATP_RETRY        1
  6. #define    MAX_RESP_SIZE    512
  7.  
  8. #define NBP_BUFFER_SIZE    4096
  9.  
  10. #define NBP_SERVER_TYPE    "Packet Monster"
  11.  
  12. static nbpParam    nbp_iopb;
  13. static namesTableEntry *nameEntry;
  14. static byte *lookupBuffer = (byte *)0;
  15.  
  16. static word atpSocket;
  17. static at_BDSType    bds[8];
  18.  
  19. static byte requestBuff[MAX_RESP_SIZE];
  20. static byte responseBuff[MAX_RESP_SIZE];
  21.  
  22. extern word theSize, theDelay;
  23. extern char theName[];
  24. extern char theZone[];
  25. extern uword theTestSize;
  26. extern word theBDSCount;
  27.  
  28. InitThem()
  29. {
  30.     /* open appletalk drivers */
  31.     OpenAppleTalk();
  32. }
  33.  
  34. SendThem()
  35. {
  36.     NetAddress addr;
  37.     word respSize, i, result;
  38.     ulong requests = 0,
  39.           packets = 0,
  40.           errors = 0;
  41.     ulong bytes = 0;
  42.     ulong time, time2, time3;
  43.     double seconds;
  44.     ulong maxbytes = theTestSize * 1024L;
  45.     
  46.     if (FindServer(&addr))
  47.         {
  48.         for (i = 0; i < theSize; i++)
  49.             requestBuff[i] = i;
  50.  
  51.         printf("server at net %d, node %d, socket %d\n",
  52.                 addr.na_network, addr.na_node, addr.na_socket);
  53.  
  54.         printf("test size %ld bytes\n", maxbytes);
  55.  
  56.         GetDateTime(&time2);
  57.         time = Ticks;
  58.         while (!Button())
  59.             {
  60.             result = SendATPReq(&addr, requests++, requestBuff, theSize, &respSize);
  61.             if (result != 0)
  62.                 errors++;
  63.             
  64.             if (theDelay)
  65.                 {
  66.                 ulong t;
  67.                 
  68.                 t = Ticks;
  69.                 while (t+theDelay >= Ticks);
  70.                 }
  71.             
  72.             bytes += theSize + respSize;
  73.             if (bytes >= maxbytes)
  74.                 break;
  75.             }
  76.  
  77.         time = Ticks - time;
  78.         GetDateTime(&time3);
  79.         time3 -= time2;
  80.         
  81.         if (requests)
  82.             {
  83.             printf("Request size %d, Response size %d\n",
  84.                     theSize, theBDSCount * MAX_RESP_SIZE);
  85.             printf("requests %ld, errors %ld --> %d%%\n",
  86.                     requests, errors, (word)((errors/requests)*100.0));
  87.                     
  88.             packets = requests * (theBDSCount + 1);
  89.             }
  90.             
  91.         seconds = time / 60.0;
  92.         
  93.         /* low mem ticks */
  94.         printf("ticks seconds %.02f\n", seconds);
  95.  
  96.         printf("  %ld bytes, %.03f seconds --> %.02f bytes/second\n",
  97.             bytes, seconds, bytes / seconds);
  98.         printf("  %ld packets --> %.02f packets/second\n",
  99.             packets, packets / seconds);
  100.  
  101.         /* clock chip */
  102.         printf("clock seconds %ld\n", time3);
  103.         seconds = time3;
  104.  
  105.         printf("  %ld bytes, %.03f seconds --> %.02f bytes/second\n",
  106.             bytes, seconds, bytes / seconds);
  107.         printf("  %ld packets --> %.02f packets/second\n",
  108.             packets, packets / seconds);
  109.         }
  110.     else
  111.         printf("failed to find a server\n");
  112.     
  113.     if (nbp_iopb.ntQElPtr != 0)
  114.         {
  115.         DisposPtr(nbp_iopb.ntQElPtr);
  116.         nbp_iopb.ntQElPtr = 0;
  117.         }
  118.  
  119. }
  120.  
  121. ReceiveThem()
  122. {
  123.     NetAddress addr;
  124.     word respSize, i, result;
  125.     ulong packets = 0,
  126.             errors = 0;
  127.     ulong user;
  128.     uword tid;
  129.  
  130.     if (atpSocket == 0)
  131.         atpSocket = ATPOpenSkt(0);
  132.  
  133.     addr.na_network = 0;
  134.     addr.na_node = 0;
  135.     addr.na_socket = atpSocket;
  136.     
  137.     if (MakeServer(&addr))
  138.         {
  139.         printf("waiting for packets...\n");
  140.  
  141.         while (!Button())
  142.             {
  143.             result = GetATPReq(&addr, &user, responseBuff, MAX_RESP_SIZE, &respSize, &tid);
  144.             if (respSize == -1)
  145.                 break;
  146.  
  147.             if (result != 0)
  148.                 errors++;
  149.             else
  150.                 {
  151.                 SendATPResp(&addr, tid, responseBuff, MAX_RESP_SIZE);
  152.                 }
  153.                 
  154.             packets++;
  155.             }
  156.  
  157.         if (packets > 0)
  158.         printf("packets %ld, errors %ld --> %d%%\n",
  159.                 packets, errors, (word)((errors/packets)*100.0));
  160.         
  161.         KillServer();
  162.         }
  163.     else
  164.         printf("unable to register server\n");
  165.  
  166.     if (nbp_iopb.ntQElPtr != 0)
  167.         {
  168.         DisposPtr(nbp_iopb.ntQElPtr);
  169.         nbp_iopb.ntQElPtr = 0;
  170.         }
  171. }
  172.  
  173. byte *MakeEntity(object, type, zone)
  174. byte *object, *type, *zone;
  175. {
  176.     byte *entity;
  177.     byte *p;
  178.     int nameSize;
  179.  
  180.     nameSize = strlen(object) + strlen(type) + strlen(zone) + 3;
  181.     p = entity = (byte *)NewPtr( (long)nameSize );
  182.     
  183.     if (entity)
  184.         {
  185.         ctopcpy(p, object);
  186.         p += strlen(object) + 1;
  187.         
  188.         ctopcpy(p, type);
  189.         p += strlen(type) + 1;
  190.         
  191.         ctopcpy(p, zone);
  192.         p += strlen(zone) + 1;
  193.         }
  194.  
  195.     return entity;
  196. }
  197.  
  198. FindServer(addr)
  199. NetAddress *addr;
  200. {
  201.     word result;
  202.     extern byte *MakeEntity();
  203.     extern NBPComplete();
  204.  
  205.     nbp_iopb.ioCompletion = 0;
  206.     nbp_iopb.ioRefNum = MPP_REFNUM;
  207.     nbp_iopb.csCode = lookupName;
  208.     nbp_iopb.interval = 5;
  209.     nbp_iopb.count = 2;
  210.     
  211.     printf("looking in zone '%s' for server '%s'\n", theZone, theName);
  212.     if (nbp_iopb.ntQElPtr == 0)
  213.         nbp_iopb.ntQElPtr = MakeEntity(theName, NBP_SERVER_TYPE, theZone);
  214.  
  215.     if (lookupBuffer == (byte *)0)
  216.         lookupBuffer = (byte *)NewPtr( (long)NBP_BUFFER_SIZE );
  217.  
  218.     nbp_iopb.nbp.lookup.retBuffPtr = lookupBuffer;
  219.     nbp_iopb.nbp.lookup.retBuffSize = NBP_BUFFER_SIZE;
  220.     nbp_iopb.nbp.lookup.maxToGet = 1000;
  221.     nbp_iopb.nbp.lookup.numGotten = 0;
  222.     
  223.     printf("looking for server...\n");
  224.     result = PBControl(&nbp_iopb, FALSE);
  225.     if (result != noErr)
  226.         return FALSE;
  227.     
  228.     printf("done, found %d servers\n",
  229.             nbp_iopb.nbp.lookup.numGotten);
  230.     
  231.     {
  232.     int i;
  233.     int newCount = 0;
  234.     byte *p = lookupBuffer;
  235.     byte *name, *object, *zone;
  236.     word network;
  237.     byte node, socket;
  238.     
  239.     for (i = 0; i < nbp_iopb.nbp.lookup.numGotten; i++)
  240.         {
  241.         network = (p[0] << 8) | p[1];
  242.         node = p[2];
  243.         socket = p[3];
  244.         name = p + 5;
  245.         object = name + name[0] + 1;
  246.         zone = object + object[0] + 1;
  247.  
  248.         if (1)
  249.             {
  250.             addr->na_network = network;
  251.             addr->na_node = node;
  252.             addr->na_socket = socket;
  253.             return TRUE;
  254.             }
  255.         
  256.         p = zone + zone[0] + 1;
  257.         }
  258.     }
  259.  
  260.     return FALSE;
  261. }
  262.  
  263. MakeServer(addr)
  264. NetAddress *addr;
  265. {
  266.     word result, i;
  267.     Handle nameHandle;
  268.     byte name[256];
  269.     
  270.     extern namesTableEntry *MakeNamesTableEntry();
  271.     
  272.     nbp_iopb.ioCompletion = 0;
  273.     nbp_iopb.ioRefNum = MPP_REFNUM;
  274.     nbp_iopb.csCode = registerName;
  275.     nbp_iopb.interval = 10;
  276.     nbp_iopb.count = 1;
  277.  
  278.     /* get user's chooser name for packet server name */
  279.     nameHandle = GetResource('STR ', -16096);
  280.     if (nameHandle == 0 || (*nameHandle)[0] == 0)
  281.         strcpy(name, NBP_SERVER_TYPE);
  282.     else
  283.         ptoccpy(name, *nameHandle);
  284.  
  285.     /* register a name, bound to a ddp socket */
  286.     nameEntry = MakeNamesTableEntry(name, NBP_SERVER_TYPE, "*");
  287.     
  288.     nameEntry->network = addr->na_network;
  289.     nameEntry->node = addr->na_node;
  290.     nameEntry->socket = addr->na_socket;
  291.  
  292.     nbp_iopb.ntQElPtr = (byte *)nameEntry;
  293.     nbp_iopb.nbp.verifyFlag = -1;
  294.  
  295.     printf("registering server\n");
  296.     result = PBControl(&nbp_iopb, FALSE);
  297.     if (result != noErr)
  298.         return FALSE;
  299.  
  300.     printf("done\n");
  301.     return TRUE;
  302. }
  303.  
  304. KillServer()
  305. {    
  306.     nbpParam    iopb;
  307.     word result, i;    
  308.     
  309.     iopb.ioCompletion = 0;
  310.     iopb.ioRefNum = MPP_REFNUM;
  311.     iopb.csCode = removeName;
  312.     iopb.ntQElPtr = nbp_iopb.ntQElPtr + 9;
  313.  
  314.     printf("removing server\n");
  315.     result = PBControl(&iopb, FALSE);
  316.     if (result != noErr)
  317.         {
  318.         printf("error %d\n", result);
  319.         return FALSE;
  320.         }
  321.  
  322.     printf("done\n");
  323.     return TRUE;
  324. }
  325.  
  326. SendATPReq(addr, user, pData, size, responseSize)
  327. NetAddress *addr;
  328. ulong user;
  329. byte *pData;
  330. word size;
  331. word *responseSize;
  332. {
  333.     word result;
  334.     atpParam iopb;
  335.     register atpParam *pb = &iopb;
  336.     register at_BDSType *b = bds;
  337.     register int i;
  338.  
  339.     *responseSize = 0;
  340.  
  341.     if (atpSocket == 0)
  342.         atpSocket = ATPOpenSkt(0);
  343.  
  344.     pb->ioRefNum = ATP_REFNUM;
  345.     pb->csCode = sendRequest;
  346.     pb->addrBlk.network = addr->na_network;
  347.     pb->addrBlk.node = addr->na_node;
  348.     pb->addrBlk.socket = addr->na_socket;
  349.     
  350.     pb->atpSkt = atpSocket;
  351.     pb->atpFlgs = 0;
  352.     pb->userData = user;
  353.  
  354.     pb->sendReqUnion.reqLength = size;
  355.     pb->sendReqUnion.reqPointer = pData;
  356.  
  357.     pb->sendReqUnion.bdsPointer = bds;
  358.     pb->sendReqUnion.numOfBuffs = theBDSCount;
  359.     pb->sendReqUnion.timeOutVal = ATP_TIMEOUT;
  360.     pb->sendReqUnion.retryCount = ATP_RETRY;
  361.  
  362.     for (i = 0; i < theBDSCount; i++, b++)
  363.         {    
  364.         b->bsdBuffSz = MAX_RESP_SIZE;
  365.         b->bdsBuffAddr = responseBuff;
  366.         b->bdsDataSz = 0;
  367.         }
  368.         
  369.     result = PBControl(&iopb, FALSE);
  370.     
  371.     for (i = 0, b = bds; i < theBDSCount; i++, b++)
  372.         {    
  373.         *responseSize += b->bdsDataSz;
  374.         }
  375.  
  376.     return result;
  377. }
  378.  
  379. GetATPReq(addr, user, pData, size, requestSize, tid)
  380. NetAddress *addr;
  381. ulong *user;
  382. byte *pData;
  383. word size;
  384. word *requestSize, *tid;
  385. {
  386.     word result;
  387.     atpParam iopb;
  388.     register atpParam *pb = &iopb;
  389.     
  390.     *requestSize = 0;
  391.     *tid = 0;
  392.  
  393.     if (atpSocket == 0)
  394.         atpSocket = ATPOpenSkt(0);
  395.  
  396.     pb->ioRefNum = ATP_REFNUM;
  397.     pb->ioCompletion = 0;
  398.     pb->csCode = getRequest;
  399.  
  400.     pb->addrBlk.network = 0;
  401.     pb->addrBlk.node = 0;
  402.     pb->addrBlk.socket = 0;
  403.     
  404.     pb->atpSkt = atpSocket;
  405.     pb->atpFlgs = 0;
  406.  
  407.     pb->getReqUnion.reqLength = size;
  408.     pb->getReqUnion.reqPointer = pData;
  409.  
  410.     result = PBControl(&iopb, TRUE);
  411.     while (pb->ioResult > 0)
  412.         {
  413.         if (Button())
  414.             {
  415.             atpParam iopbKill;
  416.             
  417.             /* kill pending getRequest */
  418.             iopbKill.ioRefNum = ATP_REFNUM;
  419.             iopbKill.csCode = killGetReq;
  420.             iopbKill.killReqUnion.killQEl = (struct atpRBlk *)&iopb;
  421.  
  422.             result = PBControl(&iopbKill, FALSE);
  423.             printf("kill getRequest result %d\n", result);
  424.             *requestSize = -1;
  425.             return 0;
  426.             }
  427.         }
  428.     
  429.     *requestSize = pb->getReqUnion.reqLength;
  430.     *tid = pb->getReqUnion.tid;
  431.     
  432.     addr->na_network = pb->addrBlk.network;
  433.     addr->na_node = pb->addrBlk.node;
  434.     addr->na_socket = pb->addrBlk.socket;
  435.  
  436.     return result;
  437. }
  438.  
  439. SendATPResp(addr, tid, pData, size)
  440. NetAddress *addr;
  441. uword tid;
  442. byte *pData;
  443. word size;
  444. {
  445.     word result;
  446.     atpParam iopb;
  447.     register atpParam *pb = &iopb;
  448.     register at_BDSType *b = bds;
  449.     register int i;
  450.     
  451.     if (atpSocket == 0)
  452.         atpSocket = ATPOpenSkt(0);
  453.  
  454.     pb->ioRefNum = ATP_REFNUM;
  455.     pb->csCode = sendResponse;
  456.     
  457.     pb->addrBlk.network = addr->na_network;
  458.     pb->addrBlk.node = addr->na_node;
  459.     pb->addrBlk.socket = addr->na_socket;
  460.     
  461.     pb->atpSkt = atpSocket;
  462.     pb->atpFlgs = atpEOM;
  463.  
  464.     pb->sendRespUnion.bdsPointer = bds;
  465.     pb->sendRespUnion.numOfBuffs = theBDSCount;
  466.     pb->sendRespUnion.bdsSize = theBDSCount;
  467.     pb->sendRespUnion.tid = tid;
  468.  
  469.     for (i = 0; i < theBDSCount; i++, b++)
  470.         {    
  471.         b->bsdBuffSz = MAX_RESP_SIZE;
  472.         b->bdsBuffAddr = responseBuff;
  473.         b->bdsDataSz = 0;
  474.         }
  475.         
  476.     result = PBControl(&iopb, FALSE);
  477.     
  478.     return result;
  479. }
  480.  
  481. OpenAppleTalk()
  482. {
  483.     word *myABusVar, refnum;
  484.     myABusVar = (word *)0x2d8;
  485.  
  486.     OpenDriver("\p.MPP", &refnum);
  487.  
  488.     if( *myABusVar == -1 )
  489.         {
  490.         SysBeep(10);
  491.         printf("Can't open AppleTalk drivers\n");
  492.         }
  493. }
  494.  
  495. int ATPOpenSkt(socket)
  496. word socket;
  497. {
  498.     word result;
  499.     atpParam iopb;
  500.     
  501.     iopb.ioRefNum = ATP_REFNUM;
  502.     iopb.csCode = openATPSkt;
  503.     iopb.atpSkt = socket;
  504.     iopb.addrBlk.network = 0;
  505.     iopb.addrBlk.node = 0;
  506.     iopb.addrBlk.socket = 0;
  507.     
  508.     result = PBControl(&iopb, FALSE);
  509.     if (result == noErr)
  510.         return iopb.atpSkt;
  511.  
  512.     return 0;
  513. }
  514.  
  515. namesTableEntry *MakeNamesTableEntry(object, type, zone)
  516. byte *object, *type, *zone;
  517. {
  518.     namesTableEntry *entry;
  519.     byte *p;
  520.     int nameSize;
  521.  
  522.     nameSize = strlen(object) + strlen(type) + strlen(zone) + 3;
  523.     entry = (namesTableEntry *)NewPtr( (long)sizeof(namesTableEntry) + nameSize );
  524.     
  525.     if (entry)
  526.         {
  527.         p = entry->names;
  528.         ctopcpy(p, object);
  529.         p += strlen(object) + 1;
  530.         
  531.         ctopcpy(p, type);
  532.         p += strlen(type) + 1;
  533.         
  534.         ctopcpy(p, zone);
  535.         p += strlen(zone) + 1;
  536.         }
  537.  
  538.     return entry;
  539. }
  540.  
  541.